//
//  CRBRotateBoxCustomAddVC.m
//  ChampsARotate
//
//  Created by Login.Fan on 2018/11/1.
//  Copyright © 2018 Logan.Fan. All rights reserved.
//

#import "CRBRotateBoxCustomAddVC.h"

#import "CRBRotateBoxCustomBrandVC.h"
#import "CRBRotateBoxCustomSeriesVC.h"
#import "CRBRotateBoxCustomTypeVC.h"

#import "CRBRotateBoxCustomChooseManage.h"
#import "CRBBoxHomeHttp.h"
#import "UIColor+CRBColor.h"

typedef NS_ENUM(NSUInteger, FuncBtnType) {
    FuncBtnType_Normal = 1,
    FuncBtnType_Selected,
    FuncBtnType_ErrorSelect,
};

@interface CRBRotateBoxCustomAddVC ()

@property (weak, nonatomic) IBOutlet UIImageView *watchImgView;
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *funcBtnArr;
@property (strong, nonatomic) IBOutletCollection(UIImageView) NSArray *arrowImgViewArr;
@property (weak, nonatomic) IBOutlet UIButton *finishedBtn;
@property (weak, nonatomic) IBOutlet UILabel *unknowTipLabel;

@property (nonatomic,copy) NSString *goodName;
@property (nonatomic,copy) NSString *goodId;
@property (nonatomic,copy) NSString *brandName;
@property (nonatomic,copy) NSString *brandId;
@property (nonatomic,copy) NSString *seriesName;
@property (nonatomic,copy) NSString *seriesId;
@property (nonatomic,copy) NSString *coverImg;
@property (nonatomic,copy) NSString *logo;

@end

@implementation CRBRotateBoxCustomAddVC

#pragma mark - 生命周期

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initUI];
    [self initData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
    
}

- (void)viewWillDisappear:(BOOL)animated {[super viewWillDisappear:animated];}

- (void)dealloc {}

#pragma mark - 设置界面
- (void)initUI {
    self.navigationItem.title = @"添加腕表";

    for (UIButton *btn in self.funcBtnArr) {
        btn.layer.borderWidth = 1;
        btn.layer.cornerRadius = 8.f;
        btn.clipsToBounds = YES;
        [self resetBtn:btn style:FuncBtnType_Normal];
    }
    [self viewAddRounderCornerWithRadius:self.watchImgView.mj_h / 2.0];
    if (_imageUrl) {
        NSString *url = [NSString stringWithFormat:@"%@%@",RootUrlImage,_imageUrl];
        [self.watchImgView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"home_watchunknown"]];
    }
}

/** 设置圆角*/
- (void)viewAddRounderCornerWithRadius:(CGFloat)radius {
    [self viewAddRounderCornerWithRadius:radius borderWidth:0 borderColor:nil];
}

/** 设置圆角和边框*/
- (void)viewAddRounderCornerWithRadius:(CGFloat)radius borderWidth:(CGFloat)width borderColor:(UIColor *)color {
    
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, self.watchImgView.mj_w, self.watchImgView.mj_h) cornerRadius:radius];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    
    if (radius != 0) {
        maskLayer.path = bezierPath.CGPath;
    }
    maskLayer.frame = CGRectMake(0, 0, self.watchImgView.mj_w, self.watchImgView.mj_h);
    
    if (width != 0 && color) {
        CAShapeLayer *borderLayer = [CAShapeLayer layer];
        borderLayer.frame = CGRectMake(0, 0, self.watchImgView.mj_w, self.watchImgView.mj_h);
        borderLayer.lineWidth = width;
        borderLayer.strokeColor = color.CGColor;
        borderLayer.fillColor = [UIColor clearColor].CGColor;
        borderLayer.path = bezierPath.CGPath;
        [self.watchImgView.layer insertSublayer:borderLayer atIndex:0];
    }
    [self.watchImgView.layer setMask:maskLayer];
}
#pragma mark - 初始化数据
- (void)initData {
    // 单例数据重置
    [[CRBRotateBoxCustomChooseManage shareManager] resetData];
}

#pragma mark - 其他方法
/** 设置btn外观*/
- (void)resetBtn:(UIButton *)btn style:(FuncBtnType)type {
    NSInteger index = [self.funcBtnArr indexOfObject:btn];
    UIImageView *arrowImg = [self.arrowImgViewArr objectAtIndex:index];
    switch (type) {
        case FuncBtnType_Normal: {
            btn.layer.borderColor = [UIColor.fontColorDarkGrey colorWithAlphaComponent:.5].CGColor;
            [btn setTitleColor:[UIColor.fontColorDarkGrey colorWithAlphaComponent:.5] forState:UIControlStateNormal];
            arrowImg.alpha = .5;
            btn.selected = NO;
        } break;
        case FuncBtnType_Selected: {
            btn.layer.borderColor = UIColor.fontColorDarkGrey.CGColor;
            [btn setTitleColor:UIColor.fontColorDarkGrey forState:UIControlStateNormal];
            arrowImg.alpha = 1;
            btn.selected = YES;
        } break;
        case FuncBtnType_ErrorSelect: {
            btn.layer.borderColor = UIColor.fontLowBatteryRad.CGColor;;
            arrowImg.alpha = .5;
            btn.selected = NO;
        } break;
        default:
            break;
    }
}

/** 品牌*/
- (IBAction)brandBtnClick:(UIButton *)sender {
    CRBRotateBoxCustomBrandVC *vc = [[CRBRotateBoxCustomBrandVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

/** 系列*/
- (IBAction)seriesBtnClick:(UIButton *)sender {
    UIButton *brandBtn = [self.funcBtnArr objectAtIndex:0];
    if (brandBtn.selected == NO) {
        [self resetBtn:brandBtn style:FuncBtnType_ErrorSelect];
        return;
    }
    CRBRotateBoxCustomSeriesVC *vc = [[CRBRotateBoxCustomSeriesVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

/** 型号*/
- (IBAction)typeBtnClick:(UIButton *)sender {
    
    UIButton *brandBtn = [self.funcBtnArr objectAtIndex:0];
    if (brandBtn.selected == NO) {
        [self resetBtn:brandBtn style:FuncBtnType_ErrorSelect];
        return;
    }
    
    UIButton *seriesBtn = [self.funcBtnArr objectAtIndex:1];
    if (seriesBtn.selected == NO) {
        [self resetBtn:seriesBtn style:FuncBtnType_ErrorSelect];
        return;
    }
    CRBRotateBoxCustomTypeVC *vc = [[CRBRotateBoxCustomTypeVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)finishedClick:(id)sender {
    
    UIButton *seriesBtn = [self.funcBtnArr objectAtIndex:1];
    if (seriesBtn.selected == NO) {
        return;
    }
    
    if ([CRBRotateBoxCustomChooseManage shareManager].goodsIndexPath) {
        
        goodVOsModel *model = [CRBRotateBoxCustomChooseManage shareManager].goodsInfoModel;
        [CRBBoxHomeHttp requestAddWatch:model customCoverImg:_imageUrl :^(NSURLSessionDataTask *task, id responseObject) {
//            WatchModel *addWatchModel = (WatchModel *)responseObject;
//            [CRBRotateBoxHomeModel defaultManager].watchModel = addWatchModel;
//            [CRBRotateBoxHomeModel defaultManager].boxWatchStyle = CRBRotateBoxWatchStyle_Selected;
//            [[CRBRotateBoxHomeModel defaultManager] saveBoxHomeModelToLibrary:[CRBRotateBoxHomeModel defaultManager]];
//            [[NSNotificationCenter defaultCenter]postNotificationName:CRBNotification_AddWathchSuccess object:nil];
            [self.navigationController popToRootViewControllerAnimated:YES];
            // 同步watchmodel到主页接口
//            if (addWatchModel.id) {
//                [CRBBoxHomeHttp requestRotateBoxHomePageData:^(NSURLSessionDataTask *task, id responseObject) {
//
//                } fail:^(NSURLSessionDataTask *task, NSError *error, NSString *code, NSString *msg) {
//
//                }];
//            } else {
//                NSLog(@"获取腕表id失败");
//            }
            [SVProgressHUD showSuccessWithStatus:@"腕表添加成功!"];
            [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeGradient];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.25f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [SVProgressHUD dismiss];
            });
            
        } fail:^(NSURLSessionDataTask *task, NSError *error, NSString *code, NSString *msg) {
//            [TheXXSystemAlert alertFrom:self alertTitle:[NSString stringWithFormat:@"%@",msg] defaultTitle:@"确定" actionType:UIAlertActionStyleDefault defaultHandler:^{
//                NSLog(@"code:%@ msg:%@",[NSString stringWithFormat:@"%@",code],[NSString stringWithFormat:@"%@",msg] );
//            }];
        }];
    } else if ([CRBRotateBoxCustomChooseManage shareManager].customGoodsName) {
        
//        [CRBBoxHomeHttp requestAddCustomWatch:[CRBRotateBoxCustomChooseManage shareManager].brandInfoModel seriesName:[CRBRotateBoxCustomChooseManage shareManager].customSeriesName goodsName:[CRBRotateBoxCustomChooseManage shareManager].customGoodsName customCoverImg:_imageUrl success:^(NSURLSessionDataTask *task, id responseObject) {
//            WatchModel *addWatchModel = (WatchModel *)responseObject;
//            [CRBRotateBoxHomeModel defaultManager].watchModel = addWatchModel;
//            [CRBRotateBoxHomeModel defaultManager].boxWatchStyle = CRBRotateBoxWatchStyle_Selected;
//            [[CRBRotateBoxHomeModel defaultManager] saveBoxHomeModelToLibrary:[CRBRotateBoxHomeModel defaultManager]];
//            [[NSNotificationCenter defaultCenter]postNotificationName:CRBNotification_AddWathchSuccess object:nil];
//            [self.navigationController popToRootViewControllerAnimated:YES];
//            // 同步watchmodel到主页接口
//            if (addWatchModel.id) {
//                [CRBBoxHomeHttp requestRotateBoxHomePageData:^(NSURLSessionDataTask *task, id responseObject) {
//
//                } fail:^(NSURLSessionDataTask *task, NSError *error, NSString *code, NSString *msg) {
//
//                }];
//            } else {
//                NSLog(@"获取腕表id失败");
//            }
//            [SVProgressHUD showSuccessWithStatus:@"腕表添加成功!"];
//            [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeGradient];
//            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.25f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//                [SVProgressHUD dismiss];
//            });
//        } fail:^(NSURLSessionDataTask *task, NSError *error, NSString *code, NSString *msg) {
//            [TheXXSystemAlert alertFrom:self alertTitle:[NSString stringWithFormat:@"%@",msg] defaultTitle:@"确定" actionType:UIAlertActionStyleDefault defaultHandler:^{
//                NSLog(@"code:%@ msg:%@",[NSString stringWithFormat:@"%@",code],[NSString stringWithFormat:@"%@",msg] );
//            }];
//        }];
    }
}

/** 刷新选择视图*/
- (void)refreshChooseView {
    _unknowTipLabel.hidden = YES;
    UIButton *btn1 = [self.funcBtnArr objectAtIndex:0];
    UIButton *btn2 = [self.funcBtnArr objectAtIndex:1];
    UIButton *btn3 = [self.funcBtnArr objectAtIndex:2];
    
    if ([CRBRotateBoxCustomChooseManage shareManager].brandIndexPath) {
        [btn1 setTitle:[CRBRotateBoxCustomChooseManage shareManager].brandInfoModel.brandNameNative forState:UIControlStateNormal];
        [self resetBtn:btn1 style:FuncBtnType_Selected];
    }
    
    if ([CRBRotateBoxCustomChooseManage shareManager].seriesIndexPath) {
        [btn2 setTitle:[CRBRotateBoxCustomChooseManage shareManager].seriesInfoModel.seriesNameNative forState:UIControlStateNormal];
        [self resetBtn:btn2 style:FuncBtnType_Selected];
    } else {
        // 自定义系列
        if ([CRBRotateBoxCustomChooseManage shareManager].customSeriesName) {
            [btn2 setTitle:[CRBRotateBoxCustomChooseManage shareManager].customSeriesName forState:UIControlStateNormal];
            [self resetBtn:btn2 style:FuncBtnType_Selected];
        }
    }
    
    if ([CRBRotateBoxCustomChooseManage shareManager].goodsIndexPath) {
            NSURL *imageURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",RootUrlImage,[CRBRotateBoxCustomChooseManage shareManager].goodsInfoModel.goodInfo.imageDefault]];
            [_watchImgView sd_setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:@"home_watchnoselected"]];
            [btn3 setTitle:[CRBRotateBoxCustomChooseManage shareManager].goodsInfoModel.goodInfo.goodName forState:UIControlStateNormal];

        [self resetBtn:btn3 style:FuncBtnType_Selected];
    } else {
        // 自定义型号
        if ([CRBRotateBoxCustomChooseManage shareManager].customGoodsName) {
            [btn3 setTitle:[CRBRotateBoxCustomChooseManage shareManager].customGoodsName forState:UIControlStateNormal];
            [self resetBtn:btn3 style:FuncBtnType_Selected];
        }
    }
}


@end
